package com.sc.web.gwt.client.presenter;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.sc.web.gwt.client.ClientFactory;
import com.sc.web.gwt.client.event.LoginEvent;
import com.sc.web.gwt.client.place.AdministratorWidgetPlace;
import com.sc.web.gwt.client.place.AgentWidgetPlace;
import com.sc.web.gwt.client.place.LoginWidgetPlace;
import com.sc.web.gwt.client.place.RegisterWidgetPlace;
import com.sc.web.gwt.client.service.GetUserService;
import com.sc.web.gwt.client.service.GetUserServiceAsync;
import com.sc.web.gwt.client.view.loginwidget.LoginWidgetView;
import com.sc.web.gwt.shared.model.Admin;
import com.sc.web.gwt.shared.model.AgentAdmin;

public class LoginActivity extends AbstractActivity implements LoginWidgetView.Presenter {
    // Used to obtain views, eventBus, placeController
    // Alternatively, could be injected via GIN
    private ClientFactory clientFactory;
    private LoginWidgetView loginWidgetView;
    private GetUserServiceAsync getUserService = (GetUserServiceAsync) GWT.create(GetUserService.class);

    public LoginActivity(LoginWidgetPlace place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
    }

    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        loginWidgetView = clientFactory.getLoginWidgetView();
        loginWidgetView.setPresenter(this);
        panel.setWidget(loginWidgetView.asWidget());
    }

    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }

    public void clearFields() {
        loginWidgetView.clearFields();
    }

    public void login(final String userName, final String password) {
        getUserService.isAdminValid(userName, password, new AsyncCallback<Admin>() {
            public void onFailure(Throwable throwable) {
                throwable.printStackTrace();
            }

            public void onSuccess(Admin admin) {
                if (admin != null) {
                    clientFactory.getEventBus().fireEvent(new LoginEvent(admin));
                    goTo(new AdministratorWidgetPlace());
                } else {
                    getUserService.isAgentAdminValid(userName, password, new AsyncCallback<AgentAdmin>() {
                        public void onFailure(Throwable throwable) {
                            throwable.printStackTrace();
                        }

                        public void onSuccess(AgentAdmin agentAdmin) {
                            if (agentAdmin != null) {
                                if (agentAdmin.getState().equals("Suspended")) {
                                    loginWidgetView.popErrorPanel("Your account has been suspended. Please contact administrator for details.");
                                } else {
                                    clientFactory.getEventBus().fireEvent(new LoginEvent(agentAdmin));
                                    goTo(new AgentWidgetPlace());
                                }
                            } else {
                                loginWidgetView.popErrorPanel("Couldn't login, please re-enter your username/password");
                            }
                        }
                    });
                }
            }
        });

    }

    public void redirectToRegisterWidget() {
        goTo(new RegisterWidgetPlace());
    }
}
